/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.model.City;
import cc.altius.model.MasterZone;
import cc.altius.model.State;
import cc.altius.model.Zone;
import cc.altius.service.CityService;
import cc.altius.service.ZoneService;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

/**
 *
 * @author santosh
 */
@Controller
public class ZoneController {

    @Autowired
    private ZoneService zoneService;
    @Autowired
    private CityService cityService;

    @ModelAttribute("zoneList")
    public List<Zone> populateZoneList() {
        return this.zoneService.getZoneList();
    }

    @ModelAttribute("masterZoneList")
    public List<MasterZone> populateMasterZoneList() {
        return this.zoneService.getMasterZoneList();
    }

    @ModelAttribute("cityList")
    public List<City> populateCityList() {

        return this.zoneService.getCityList();

    }

    @ModelAttribute("stateList")
    public List<State> populateStateList() {
        List<State> stateList = this.cityService.getStateList();
        return stateList;
    }

    @RequestMapping(value = "/zone/addZone.htm", method = RequestMethod.GET)
    public String showAddZone(ModelMap modelMap) {
        Zone zone = new Zone();
        modelMap.addAttribute("zone", zone);
        return "zone/addZone";
    }

    @RequestMapping(value = "/zone/addZone.htm", method = RequestMethod.POST)
    public String addZone(@ModelAttribute("zone") Zone zone, Errors errors, HttpServletRequest request,
            SessionStatus sessionStatus) {
        if (this.zoneService.existZoneByZoneName(zone.getZoneName(),zone.getZoneCode())) {
            errors.rejectValue("zoneName", "msg.duplicateZoneName");
            //errors.rejectValue("zoneCode", "msg.duplicateZoneCode");
            return "/zone/addZone";
        }
        int zoneId = this.zoneService.addZone(zone);
        if (zoneId == 0) {
            errors.rejectValue("zoneName", "msg.zoneNameError");
            return "/zone/addZone";
        } else {
            sessionStatus.setComplete();
            return "redirect:../zone/listZone.htm?msg=zone added successfully..";
        }
    }

    @RequestMapping(value = "/zone/listZone.htm", method = RequestMethod.GET)
    public String showZoneList(ModelMap modelMap) {
        List<Map<String, Object>> filterZone = this.zoneService.getFilterZone(0, 0, 0);
        modelMap.addAttribute("filterZone", filterZone);
        modelMap.addAttribute("cityId", 0);
        modelMap.addAttribute("stateId", 0);
        modelMap.addAttribute("masterZoneId", 0);

        return "zone/listZone";
    }

    @RequestMapping(value = "/zone/listZone.htm", method = RequestMethod.POST)
    public String filterOnZone(@RequestParam(value = "stateId", required = true, defaultValue = "0") int stateId,
            @RequestParam(value = "cityId", required = true, defaultValue = "0") int cityId,
            @RequestParam(value = "masterZoneId", required = true, defaultValue = "0") int masterZoneId,
            ModelMap modelMap) {

        List<Map<String, Object>> filterZone = this.zoneService.getFilterZone(stateId, cityId, masterZoneId);
        modelMap.addAttribute("filterZone", filterZone);
        modelMap.addAttribute("cityId", cityId);
        modelMap.addAttribute("stateId", stateId);
        modelMap.addAttribute("masterZoneId", masterZoneId);

        return "zone/listZone";
    }

    @RequestMapping(value = "/zone/editZone.htm", method = RequestMethod.GET)
    public String ShowupdateZone(@RequestParam("zoneId") String zoneId, ModelMap modelMap) {
        Zone zone = this.zoneService.getZoneById(Integer.parseInt(zoneId));
        modelMap.addAttribute("zone", zone);
        return "zone/editZone";
    }

    @RequestMapping(value = "/zone/editZone.htm", method = RequestMethod.POST)
    public String updateZone(@ModelAttribute("zone") Zone zone) {

        int Id = this.zoneService.updateZone(zone);
        if (Id != 0) {
            return "redirect:../zone/listZone.htm?msg=update zone successfully..";
        } else {
            return "redirect:../zone/editZone.htm?msg=update zone failed..";
        }
    }
}
